const mysql = require('mysql')
const config = require('./default')

// 创建线程池
const pool = mysql.createPool({
    host: config.dataBase.HOST,
    user: config.dataBase.USERNAME,
    password: config.dataBase.PASSWORD,
    database: config.dataBase.DATABASE,
    port: config.dataBase.PORT
})

// 连接线程池，做sql查找
let allServices = {
    query: function (sql, values) {
        return new Promise((resolve, reject) => {
            pool.getConnection(function (err, connection) {
                if (err) {
                    reject(err)
                }
                else { // 连接成功
                    connection.query(sql, values, function (err, data) {
                        if (err) {
                            reject(err)
                        }
                        else {
                            resolve(data)
                        }
                        pool.releaseConnection(connection) // 释放连接
                    })
                }
            })
        })
    }
}

module.exports = allServices